package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.Environment;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.InviteFriendShare;
import com.kkd.kkdapp.http.entity.ParticularsData;
import com.kkd.kkdapp.http.entity.Tags;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Tools;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.CustomScrollView;
import com.kkd.kkdapp.view.CustomScrollView.OnRefreshListener;
import com.kkd.kkdapp.view.CustomScrollView.OnScrollListener;
import com.kkd.kkdapp.view.KKdWebview;
import com.kkd.kkdapp.view.MyProgressbar;
import com.kkd.kkdapp.view.MyRelativeLayout.OnItemClick;
import com.nostra13.universalimageloader.core.ImageLoader;

/***
 * 
 * @data 2016-2-25
 * 
 * @author mty
 * 
 * @name 标的详情
 * 
 */

public class RegularParticularsActivity extends BaseActivity implements
		OnClickListener {

	private static final int DETAILS_REQUEST_SUCCESS = 1;
	private static final int DETAILS_REQUEST_FAIL = 2;
	private static final int TO_LOGIN = 1000;

	private MyProgressbar progressbar;
	private KKdWebview webview;
	private RelativeLayout title_img;
	private int max = 100;
	private int current_progress = 0;
	private double curent_textprogress = 0.00;
	private DecimalFormat df = new DecimalFormat("###,###");
	private DecimalFormat df1 = new DecimalFormat("0.01");
	private String bidId = ""; // 标的ID
	private String bidTitle; // 标的标题
	private long bidAmount; // 标的总额
	private long totalLixi; // 总利息
	private long lowestAmount; // 　最低限制金额&起头金额
	private long amountMultiple; // 最多限制金额&倍数
	private String bidPeriodType; // 期限单位
	private String bidProgress;
	private long bidFreeAmount; // 　标的剩余金额
	private String bidInterest; // 标的年化收益
	private double interest; // 进度条进度
	private String bidStatus; // 标的状态
	private int bidPeriod; // 理财期限
	private String riskMeasure;// 风险投资url
	private String bidType; // 　501 快快享福
	private String ProjectDesc; // 项目描述url
	private long maxAmount;
	private List<Tags> tags;
	private int poicount;
	private String bidDescription;
	private TextView title_tv; // 标的标题 TextView
	private TextView interest_tv; // 年化收益 TextView
	private TextView term_tv; // 投资期限 TextView
	private TextView term_company_tv; // 投资期限单位 TextView
	private TextView purchase_amount; // 可购买金额 TextView
	private TextView total_target_tv; // 标的总额 TextView
	private TextView number_tv; // 购买人数
	private Button tender_bt; // 投标
	private RelativeLayout bid_records; // 投标记录
	private RelativeLayout project_course; // 项目历程
	private double balance; // 账户余额
	private ImageView icon_img1, icon_img2, icon_img3, icon_img4;
	private TextView icon_tv1, icon_tv2, icon_tv3, icon_tv4;
	private String appbiddetail; // 项目描述，风险措施，还款计划 h5 url
	private RelativeLayout counter_rl; // 计算器
	private CustomScrollView mScrollView; // 下拉刷新
	private RelativeLayout icon_rl1, icon_rl2, icon_rl3, icon_rl4;
	private TextView rise_tv1; // 加息
	private boolean webFlag = true;
	private long countDown;
	private String poiId = "";
	private int productType;
	private Dialog viewPagerDialog;
	private Dialog videoDialog;
	private String[] imaIdArray;
	private String[] textArray;
	private TextView number_tv1, number_tv2;
	private Timer timer = null;
	private TimerTask myTimerTask = null;
	private int progress = 0;
	private double iinterest;
	private TextView purchase_schedule_tv;
	private TextView amountMultiple_tv; // 起投金额
	private LinearLayout central;
	private int centralHeight;
	private RelativeLayout particulars_title;
	private String appbiddetail_fromIntent;
	private String bidPeriodWeb;
	private int bidPeriodAdd;
	private int isNewbie;
	private RelativeLayout icon_rl;
	private RelativeLayout novice_rl;
	private String tagsTile = "";
	private TextView guarantee_tv;
	private LinearLayout rel_content;
	private RelativeLayout rel_no_content;
	private ImageView img_null;
	private TextView tv_null;
	private boolean firstEntryFlag = true;
	private RelativeLayout par_ll;
	private boolean webFailedToLoad = false;

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case DETAILS_REQUEST_SUCCESS:
				par_ll.setVisibility(View.VISIBLE);
				firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
						RegularParticularsActivity.this, firstEntryFlag, false,
						rel_content, rel_no_content, img_null, tv_null, "", 0);
				updateUI();
				mScrollView.onRefreshComplete();
				break;
			case DETAILS_REQUEST_FAIL:
				int statusCode = (Integer) msg.obj;
				if (firstEntryFlag) {
					par_ll.setVisibility(View.GONE);
				}
				if (statusCode == KkdHttpClient.NO_NET) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							RegularParticularsActivity.this, firstEntryFlag,
							true, rel_content, rel_no_content, img_null,
							tv_null, "网络错误，请稍后重试", R.drawable.img_null_net);
				} else {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							RegularParticularsActivity.this, firstEntryFlag,
							true, rel_content, rel_no_content, img_null,
							tv_null, "服务异常，请稍后重试", R.drawable.img_null_net);
				}
				mScrollView.onRefreshComplete();
				break;
			}
		};
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (progress != 0) {
				if (progress == 99) {
					progressbar.setProgress(100);
				} else {
					progressbar.setProgress(progress);
				}
			} else {
				progressbar.setProgress((int) iinterest);
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regular_particulars);
		mTintManager.setStatusBarTintResource(0x00000000);
		initView();
		setListener();
	}

	/*****************
	 * 初始化控件
	 *****************/
	@SuppressLint("SetJavaScriptEnabled")
	public void initView() {
		title_tv = (TextView) this.findViewById(R.id.title_tv);
		interest_tv = (TextView) this.findViewById(R.id.interest_tv);
		term_tv = (TextView) this.findViewById(R.id.term_tv);
		term_company_tv = (TextView) this.findViewById(R.id.term_company_tv);
		purchase_amount = (TextView) this.findViewById(R.id.purchase_amount);
		total_target_tv = (TextView) this.findViewById(R.id.total_target_tv);
		number_tv = (TextView) this.findViewById(R.id.number_tv);
		tender_bt = (Button) this.findViewById(R.id.tender_bt);
		bid_records = (RelativeLayout) this.findViewById(R.id.bid_records);
		project_course = (RelativeLayout) this
				.findViewById(R.id.project_course);
		icon_img1 = (ImageView) this.findViewById(R.id.icon_img1);
		icon_img2 = (ImageView) this.findViewById(R.id.icon_img2);
		icon_img3 = (ImageView) this.findViewById(R.id.icon_img3);
		icon_img4 = (ImageView) this.findViewById(R.id.icon_img4);
		icon_tv1 = (TextView) this.findViewById(R.id.icon_tv1);
		icon_tv2 = (TextView) this.findViewById(R.id.icon_tv2);
		icon_tv3 = (TextView) this.findViewById(R.id.icon_tv3);
		icon_tv4 = (TextView) this.findViewById(R.id.icon_tv4);
		counter_rl = (RelativeLayout) this.findViewById(R.id.counter_rl);
		mScrollView = (CustomScrollView) this.findViewById(R.id.pt_sv);
		mScrollView.setHeaderBackgroundResource(R.color.blue_4845a4);
		mScrollView.setArrowImageView(R.drawable.xlistview_arrow_white);
		mScrollView.setGifDrawable(R.drawable.four_white_dot2);
		icon_rl1 = (RelativeLayout) this.findViewById(R.id.icon_rl1);
		icon_rl2 = (RelativeLayout) this.findViewById(R.id.icon_rl2);
		icon_rl3 = (RelativeLayout) this.findViewById(R.id.icon_rl3);
		icon_rl4 = (RelativeLayout) this.findViewById(R.id.icon_rl4);
		rise_tv1 = (TextView) this.findViewById(R.id.rise_tv1);
		number_tv1 = (TextView) this.findViewById(R.id.number_tv1);
		number_tv2 = (TextView) this.findViewById(R.id.number_tv2);

		progressbar = (MyProgressbar) this.findViewById(R.id.progressbar);
		title_img = (RelativeLayout) this.findViewById(R.id.title_img);
		progressbar.setMax(max);
		webview = (KKdWebview) this.findViewById(R.id.webview);
		webview.setFocusable(true);
		WebSettings webSettings = webview.getSettings();
		// 设置WebView属性，能够执行Javascript脚本
		webSettings.setJavaScriptEnabled(true);
		webview.getSettings().setDefaultTextEncodingName("utf-8");
		// 设置可以访问文件
		webSettings.setAllowFileAccess(true);
		// 设置支持缩放
		webSettings.setBuiltInZoomControls(true);
		webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		amountMultiple_tv = (TextView) this
				.findViewById(R.id.amountMultiple_tv);
		purchase_schedule_tv = (TextView) this
				.findViewById(R.id.purchase_schedule_tv);
		central = (LinearLayout) this.findViewById(R.id.central);
		particulars_title = (RelativeLayout) this
				.findViewById(R.id.particulars_title);
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
			LayoutParams params = particulars_title.getLayoutParams();
			params.height = SystemUtils.dip2px(this, 50);
			particulars_title.setLayoutParams(params);
		}
		icon_rl = (RelativeLayout) this.findViewById(R.id.icon_rl);
		novice_rl = (RelativeLayout) this.findViewById(R.id.novice_rl);
		guarantee_tv = (TextView) this.findViewById(R.id.guarantee_tv);
		webview.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view,
					final String url) {
				if (url.startsWith("tel:")) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse(url));
					startActivity(intent);
				} else if (url.startsWith("http:") || url.startsWith("https:")) {
					if (url.indexOf("video") > 0
							&& !SystemUtils.getNetTypeName().equals("WIFI")) {

						if (videoDialog != null) {
							videoDialog.cancel();
							videoDialog = null;
						}
						videoDialog = DialogUtils.generate2BDialog(
								RegularParticularsActivity.this,
								"您处于非WIFI环境,即将使用流量查看视频,是否继续?", "确定", "取消",
								new OnClickListener() {

									@Override
									public void onClick(View v) {
										videoDialog.dismiss();
										if (!TextUtils.isEmpty(url)) {
											Intent intent = new Intent(
													RegularParticularsActivity.this,
													KKDWebActivity.class);
											intent.putExtra("url", url);
											// intent.putExtra("titleName",
											// bidTitle);
											startActivity(intent);
										}
									}
								}, new OnClickListener() {

									@Override
									public void onClick(View v) {
										videoDialog.dismiss();

									}
								});
						videoDialog.show();

					} else {
						Intent intent = new Intent(
								RegularParticularsActivity.this,
								KKDWebActivity.class);
						intent.putExtra("url", url);
						// intent.putExtra("titleName", bidTitle);
						startActivity(intent);
					}
				}
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				webFailedToLoad = true;
			};

			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			}
		});

		rel_content = (LinearLayout) this.findViewById(R.id.rel_content);
		rel_no_content = (RelativeLayout) this
				.findViewById(R.id.rel_no_content);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);
		par_ll = (RelativeLayout) this.findViewById(R.id.par_ll);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		centralHeight = central.getHeight();
	}

	/***********************
	 * 获取上个界面传过来的消息
	 ***********************/
	public void getMessage() {
		Intent intent = this.getIntent();
		bidId = intent.getStringExtra("bidId");
		poiId = intent.getStringExtra("poiId");
		appbiddetail_fromIntent = intent.getStringExtra("appbiddetail");
		// h5url
		LogUtil.e("appbiddetail", appbiddetail);
		if (webFlag) {
			if (!TextUtils.isEmpty(appbiddetail_fromIntent)) {
				if (PreferenceUtils.getInstance().getLogin(this) == 0) {
					String customerIdAndToken = PreferenceUtils.getInstance()
							.getcustomerId(this);
					String[] cat = customerIdAndToken.split("&");
					webview.loadUrl(appbiddetail_fromIntent + "&poiId=" + poiId
							+ "&customerId=" + cat[1] + "&token=" + cat[0]);
				} else {
					webview.loadUrl(appbiddetail_fromIntent + "&poiId=" + poiId
							+ "&customerId=&token=");
				}
			}
		}
		// 请求接口
		requestParticulars();
		mScrollView.onRefreshComplete();
	}

	@Override
	protected void onResume() {
		super.onResume();
		getMessage();
		if (PreferenceUtils.getInstance().getLogin(this) == 0) {
			getAccountInfo();
		}
	}

	/****************
	 * 注册监听事件
	 ****************/
	public void setListener() {
		title_img.setOnClickListener(this);
		bid_records.setOnClickListener(this);
		project_course.setOnClickListener(this);
		tender_bt.setOnClickListener(this);
		counter_rl.setOnClickListener(this);
		icon_rl1.setOnClickListener(this);
		icon_rl2.setOnClickListener(this);
		icon_rl3.setOnClickListener(this);
		icon_rl4.setOnClickListener(this);
		mScrollView.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				webFlag = true;
				appbiddetail_fromIntent = "";
				requestParticulars();
			}
		});
		mScrollView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScroll(int scrollY) {
				if (scrollY >= centralHeight) {
					particulars_title.setBackgroundColor(Color
							.parseColor("#4845a4"));
				} else {
					particulars_title.setBackgroundResource(R.drawable.top_bg);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_img: // 　后退
			finish();
			break;
		case R.id.bid_records: // 投标记录
			SystemUtils.onEvent("Regular_Detail_Record", 1);
			intent = new Intent(this, ConditionActivity.class);
			intent.putExtra("bidId", bidId);
			startActivity(intent);
			break;
		case R.id.project_course: // 项目历程
			SystemUtils.onEvent("Regular_Detail_Process", 1);
			if (bidId != null) {
				intent = new Intent(this, KKDWebActivity.class);
				intent.putExtra("url", Environment.getH5Url()
						+ "itemPro.html?bidid=" + bidId);
				intent.putExtra("titleName", "项目历程");
				startActivity(intent);

			}
			break;
		case R.id.tender_bt: // 　投标
			SystemUtils.onEvent("Regular_Detail_Bid", 1);
			if ("分享".equals(tender_bt.getText().toString())) {
				InviteFriendShare inviteFriendShare = PreferenceUtils
						.getInstance().getInviteFriend(this);
				PreferenceUtils.getInstance().setH5Entrance(this, "邀请好友攻略");
				toShareURL(this, inviteFriendShare.getInviteFriendUrl(),
						inviteFriendShare.getTitle(),
						inviteFriendShare.getDesc(),
						inviteFriendShare.getImageUrl());
			} else {
				tenderButton();
			}

			break;
		case R.id.counter_rl: // 计算器
			SystemUtils.onEvent("Regular_Detail_Calculator", 1);
			if (AppUtil.isConnect(this)) {
				LogUtil.e("TAG", "" + bidPeriodAdd);
				DialogUtils.showCalculateWindow(this, v, bidPeriodType,
						bidFreeAmount, bidAmount, totalLixi, bidPeriod,
						bidPeriodAdd);
				softKeyboard();
			}
			break;
		case R.id.icon_rl1:
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(1, imaIdArray, textArray);
			}
			break;
		case R.id.icon_rl2:
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(2, imaIdArray, textArray);
			}
			break;
		case R.id.icon_rl3:
			// if (tags.size() == 3) {
			// intent = new Intent(this, KKDWebActivity.class);
			// intent.putExtra("url", com.kkd.kkdapp.Environment.getH5Url()
			// + "safety.html"); // 正式
			// intent.putExtra("titleName", "安全保障");
			// startActivity(intent);
			// }
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(3, imaIdArray, textArray);
			}
			break;
		case R.id.icon_rl4:
			// if (tags.size() == 4) {
			// intent = new Intent(this, KKDWebActivity.class);
			// intent.putExtra("url", com.kkd.kkdapp.Environment.getH5Url()
			// + "safety.html"); // 正式
			// intent.putExtra("titleName", "安全保障");
			// startActivity(intent);
			// }
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(4, imaIdArray, textArray);
			}
			break;
		}

	}

	/*******
	 * 投标button跳转
	 *******/
	public void tenderButton() {
		Intent intent;
		if ("5002".equals(bidStatus)) {

			if (PreferenceUtils.getInstance().getLogin(this) == 0) { // 登陆验证

				if (PreferenceUtils.getInstance().getAutonym(this) == 1) { // 身份认证

					if (PreferenceUtils.getInstance().getBoundBankCard(
							RegularParticularsActivity.this) == 1) { // 判定银行卡是否绑定
						intent = new Intent(this, TenderAffirmActivity.class);
						intent.putExtra("bidId", bidId);
						intent.putExtra("toMidFlag", "1");
						intent.putExtra("bidTitle", bidTitle);
						intent.putExtra("bidPeriod", bidPeriod);
						intent.putExtra("amountMultiple", amountMultiple);
						intent.putExtra("lowestAmount", lowestAmount);
						intent.putExtra("bidPeriodType", bidPeriodType);
						intent.putExtra("bidInterest", bidInterest);
						intent.putExtra("bidFreeAmount", bidFreeAmount);
						intent.putExtra("maxAmount", maxAmount);
						intent.putExtra("bidAmount", bidAmount);
						intent.putExtra("totalLixi", totalLixi);
						intent.putExtra("bidType", bidType);
						intent.putExtra("productType", productType);
						intent.putExtra("bidPeriodWeb", bidPeriodWeb);
						intent.putExtra("bidPeriodAdd", bidPeriodAdd);
						if (productType == 3) {
							intent.putExtra("businessType", "3");
						} else {
							intent.putExtra("businessType", "1");
						}
						intent.putExtra("isNewbie", isNewbie);
						this.startActivity(intent);
					} else {
						if (balance == 0) {
							intent = new Intent(this, ForTenderActivity.class);
							intent.putExtra("enterTypeFlag", 5);
							intent.putExtra("bidId", bidId);
							intent.putExtra("bidTitle", bidTitle);
							intent.putExtra("bidPeriod", bidPeriod);
							intent.putExtra("bidInterest", bidInterest);
							intent.putExtra("bidFreeAmount", bidFreeAmount);
							intent.putExtra("bidPeriodType", bidPeriodType);
							intent.putExtra("maxAmount", maxAmount);
							intent.putExtra("bidAmount", bidAmount);
							intent.putExtra("totalLixi", totalLixi);
							intent.putExtra("bidType", bidType);
							intent.putExtra("toMidFlag", "1");
							intent.putExtra("bidPeriodWeb", bidPeriodWeb);
							intent.putExtra("bidPeriodAdd", bidPeriodAdd);
							if (productType == 3) {
								intent.putExtra("businessType", "3");
							} else {
								intent.putExtra("businessType", "1");
							}
							intent.putExtra("productType", productType);
							intent.putExtra("isNewbie", isNewbie);
							startActivity(intent);
						} else {
							intent = new Intent(this,
									TenderAffirmActivity.class);
							intent.putExtra("bidType", bidType);
							intent.putExtra("bidId", bidId);
							intent.putExtra("bidTitle", bidTitle);
							intent.putExtra("bidPeriod", bidPeriod);
							intent.putExtra("amountMultiple", amountMultiple);
							intent.putExtra("lowestAmount", lowestAmount);
							intent.putExtra("bidPeriodType", bidPeriodType);
							intent.putExtra("bidInterest", bidInterest);
							intent.putExtra("bidFreeAmount", bidFreeAmount);
							intent.putExtra("maxAmount", maxAmount);
							intent.putExtra("bidAmount", bidAmount);
							intent.putExtra("totalLixi", totalLixi);
							intent.putExtra("toMidFlag", "1");
							intent.putExtra("bidPeriodWeb", bidPeriodWeb);
							intent.putExtra("bidPeriodAdd", bidPeriodAdd);
							if (productType == 3) {
								intent.putExtra("businessType", "3");
							} else {
								intent.putExtra("businessType", "1");
							}
							intent.putExtra("productType", productType);
							intent.putExtra("isNewbie", isNewbie);
							this.startActivity(intent);
						}

					}

				} else {
					intent = new Intent(this, ForTenderActivity.class);
					intent.putExtra("enterTypeFlag", 5);
					intent.putExtra("bidId", bidId);
					intent.putExtra("bidTitle", bidTitle);
					intent.putExtra("bidPeriod", bidPeriod);
					intent.putExtra("bidInterest", bidInterest);
					intent.putExtra("bidPeriodType", bidPeriodType);
					intent.putExtra("bidFreeAmount", bidFreeAmount);
					intent.putExtra("maxAmount", maxAmount);
					intent.putExtra("bidAmount", bidAmount);
					intent.putExtra("totalLixi", totalLixi);
					intent.putExtra("bidType", bidType);
					LogUtil.e("bidPeriod", "" + bidPeriod);
					LogUtil.e("bidPeriodType", "" + bidPeriodType);
					intent.putExtra("toMidFlag", "1");
					intent.putExtra("bidPeriodWeb", bidPeriodWeb);
					intent.putExtra("bidPeriodAdd", bidPeriodAdd);
					if (productType == 3) {
						intent.putExtra("businessType", "3");
					} else {
						intent.putExtra("businessType", "1");
					}
					intent.putExtra("productType", productType);
					intent.putExtra("isNewbie", isNewbie);
					this.startActivity(intent);
				}

			} else {
				intent = new Intent(this, LoginActivity.class);
				this.startActivity(intent);

			}
		}
	}

	/*****************
	 * 请求定期详情接口
	 ****************/
	public void requestParticulars() {
		KkdHttpMagager.getInstance().getParticularsManager(bidId,
				ParticularsData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						ParticularsData data = (ParticularsData) responseBody;
						bidId = data.getBidId();
						bidTitle = data.getBidTitle();
						bidAmount = data.getBidAmount();
						totalLixi = data.getTotalLixi();
						lowestAmount = data.getLowestAmount();
						amountMultiple = data.getAmountMultiple();
						bidPeriodType = data.getBidPeriodType();
						bidProgress = data.getBidProgress();
						LogUtil.e("bidProgress", bidProgress);
						curent_textprogress = Double.parseDouble(bidProgress);
						current_progress = (int) curent_textprogress;
						bidFreeAmount = data.getBidFreeAmount();
						bidInterest = data.getInterestDisplay();
						interest = (1 - (double) bidFreeAmount / bidAmount);
						bidStatus = data.getBidStatus();
						bidPeriod = data.getBidPeriod();
						riskMeasure = data.getRiskMeasure();
						bidType = data.getBidType();
						ProjectDesc = data.getProjectDesc();
						maxAmount = data.getMaxAmount();
						tags = data.getTags2();
						poicount = data.getPoiCount();
						bidDescription = data.getBidDescription();
						appbiddetail = data.getAppbiddetail();
						countDown = data.getCountDown();
						productType = data.getProductType();
						bidPeriodWeb = data.getBidPeriodWeb();
						bidPeriodAdd = data.getBidPeriodAdd();
						isNewbie = data.getIsNewbie();
						tagsTile = data.getTagsTile();
						mHandler.sendEmptyMessage(DETAILS_REQUEST_SUCCESS);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = statusCode;
						msg.what = DETAILS_REQUEST_FAIL;
						mHandler.sendMessage(msg);
					}
				});
	}

	/************
	 * 更新UI
	 ************/
	public void updateUI() {
		// 购买进度
		purchase_schedule_tv.setText(bidProgress + "%");
		// 起投金额
		amountMultiple_tv.setText(lowestAmount / 100 + "元");
		// 标题
		title_tv.setText(bidTitle);
		// 年化收益
		if (bidInterest.indexOf("+") == -1) {
			interest_tv.setText(Tools.intOrDouble(Double
					.parseDouble(bidInterest)) + "");
			rise_tv1.setText("%");
			rise_tv1.setBackgroundResource(0);
		} else {
			String[] bit = bidInterest.split("\\+");
			interest_tv.setText(Tools.intOrDouble(Double.parseDouble(bit[0]))
					+ "");
			rise_tv1.setText("%+"
					+ Tools.intOrDouble(Double.parseDouble(bit[1])) + "%");
			rise_tv1.setBackgroundResource(0);

		}
		// 投资期限
		term_tv.setText("" + bidPeriodWeb);
		// // 　投资期限单位
		// if ("月".equals(bidPeriodType)) {
		// term_company_tv.setText("个" + bidPeriodType);
		// } else if ("天".equals(bidPeriodType)) {
		// term_company_tv.setText(bidPeriodType);
		// }
		// 　可购买金额
		purchase_amount.setText(df.format(bidFreeAmount / 100));
		// 标的总额
		total_target_tv.setText(df.format(bidAmount / 100));
		if (tags.size() == 1) {
			icon_rl1.setVisibility(View.VISIBLE);
		} else if (tags.size() == 2) {
			icon_rl1.setVisibility(View.VISIBLE);
			icon_rl2.setVisibility(View.VISIBLE);
		} else if (tags.size() == 3) {
			icon_rl1.setVisibility(View.VISIBLE);
			icon_rl2.setVisibility(View.VISIBLE);
			icon_rl3.setVisibility(View.VISIBLE);
		} else if (tags.size() == 4) {
			icon_rl1.setVisibility(View.VISIBLE);
			icon_rl2.setVisibility(View.VISIBLE);
			icon_rl3.setVisibility(View.VISIBLE);
			icon_rl4.setVisibility(View.VISIBLE);
		}
		imaIdArray = new String[tags.size()];
		textArray = new String[tags.size()];
		for (int i = 0; i < tags.size(); i++) {
			imaIdArray[i] = tags.get(i).getImageBig();
			textArray[i] = tags.get(i).getNameBig();
			if (i == 0) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img1, R.drawable.mybank_default);
				icon_tv1.setText(tags.get(i).getName());
			} else if (i == 1) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img2, R.drawable.mybank_default);
				icon_tv2.setText(tags.get(i).getName());
			} else if (i == 2) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img3, R.drawable.mybank_default);
				icon_tv3.setText(tags.get(i).getName());
			} else if (i == 3) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img4, R.drawable.mybank_default);
				icon_tv4.setText(tags.get(i).getName());
			}
		}

		// 购买人数
		if (poicount == 0) {
			number_tv1.setVisibility(View.INVISIBLE);
			number_tv2.setVisibility(View.INVISIBLE);
			number_tv.setText("暂无投标");
		} else {
			number_tv.setText("" + poicount);
			number_tv1.setVisibility(View.VISIBLE);
			number_tv2.setVisibility(View.VISIBLE);
		}
		// 标的状态
		if (SystemUtils.isOldUser(this) && isNewbie == 1) {
			tender_bt.setEnabled(true);
			tender_bt.setText("分享");
		} else {
			if ("5001".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("新建");
			} else if ("5002".equals(bidStatus)) {
				tender_bt.setEnabled(true);
				tender_bt.setText("投标");
			} else if ("5003".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("已满标");
			} else if ("5004".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("还款中");
			} else if ("5005".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("已还款");
			} else if ("5006".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("待发布");
			} else if ("5007".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("处理中");
			} else if ("40011".equals(bidStatus)) {
				tender_bt.setEnabled(false);
				tender_bt.setText("已流标");
			}
		}
		if (webFlag) {
			webFlag = false;
			if (TextUtils.isEmpty(appbiddetail_fromIntent) || webFailedToLoad) {
				// h5url
				LogUtil.e("appbiddetail", appbiddetail);

				if (PreferenceUtils.getInstance().getLogin(
						RegularParticularsActivity.this) == 0) {
					String customerIdAndToken = PreferenceUtils.getInstance()
							.getcustomerId(RegularParticularsActivity.this);
					String[] cat = customerIdAndToken.split("&");
					webview.loadUrl(appbiddetail + "&poiId=" + poiId
							+ "&customerId=" + cat[1] + "&token=" + cat[0]);
				} else {
					webview.loadUrl(appbiddetail + "&poiId=" + poiId
							+ "&customerId=&token=");
				}
				// webview.loadUrl(appbiddetail + "?poiId = " + poiId);
			}
		}

		webview.setFocusable(false);

		iinterest = (double) (interest * 100);
		if (timer != null) {
			timer.cancel();
			timer = null;
		}

		if (myTimerTask != null) {
			myTimerTask.cancel();
			myTimerTask = null;
		}

		if (timer == null)
			timer = new Timer();
		if (myTimerTask == null) {
			myTimerTask = new TimerTask() {

				@Override
				public void run() {
					progress = progress + 1;
					if (progress <= iinterest) {
						handler.sendEmptyMessage(1);
					} else {
						if (iinterest < 1 && iinterest > 0) {
							progressbar.setProgress(1);
						}
						progress = 0;

						myTimerTask.cancel();
						myTimerTask = null;
						timer.cancel();
						timer = null;
					}

				}
			};
		}
		timer.schedule(myTimerTask, 00, 10);

		// 新手标处理
		if (isNewbie == 1) {
			icon_rl.getLayoutParams().height = SystemUtils.dip2px(this, 99);
			novice_rl.setVisibility(View.VISIBLE);
			icon_rl1.setClickable(false);
			icon_rl2.setClickable(false);
			icon_rl3.setClickable(false);
			icon_rl4.setClickable(false);
			if (tagsTile != null && !"".equals(tagsTile)) {
				guarantee_tv.setText(tagsTile);
			} else {
				guarantee_tv.setText("联合保障您的投资安全");
			}
		} else {
			icon_rl.getLayoutParams().height = SystemUtils.dip2px(this, 80);
			novice_rl.setVisibility(View.GONE);
			icon_rl1.setClickable(true);
			icon_rl2.setClickable(true);
			icon_rl3.setClickable(true);
			icon_rl4.setClickable(true);
		}

	}

	/**
	 * 
	 * 查询账户余额
	 */
	public void getAccountInfo() {

		KkdHttpMagager.getInstance().getAccountInfoManager(String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						try {
							String responseString = responseBody.toString();
							JSONObject data = new JSONObject(responseString);
							balance = data.getDouble("balance");
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
					}
				});
	}

	private void showViewPagerDialog(int currentItem,
			final String[] imaIdArray, final String[] textArray) {
		if (viewPagerDialog != null) {
			viewPagerDialog.cancel();
			viewPagerDialog = null;
		}
		viewPagerDialog = DialogUtils.showCharacteristicDialog(
				RegularParticularsActivity.this, currentItem, imaIdArray,
				textArray, new OnItemClick() {

					@Override
					public void getPosition(int position) {
						if (tags != null && tags.size() != 0
								&& "安全保障".equals(tags.get(position).getName())) {
							Intent intent = new Intent(
									RegularParticularsActivity.this,
									KKDWebActivity.class);
							intent.putExtra("url",
									com.kkd.kkdapp.Environment.getH5Url()
											+ "appinsurance.html"); // 正式
							intent.putExtra("titleName", "安全保障");
							startActivity(intent);
							if (viewPagerDialog != null) {
								viewPagerDialog.cancel();
								viewPagerDialog = null;
							}
						}
					}
				});
		viewPagerDialog.show();
	}

	public void softKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		// 得到InputMethodManager的实例
		if (imm.isActive()) {
			// 如果开启
			imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
					InputMethodManager.HIDE_NOT_ALWAYS);
			// 关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myTimerTask != null) {
			myTimerTask.cancel();
			myTimerTask = null;
		}
		if (timer != null) {
			timer.cancel();
			timer = null;
		}

	}
}
